#include <iostream>
using namespace std;

class Point
{
private:
    int x, y;
public:
    Point(int x, int y);
    ~Point();

    // virtual 运行时多态，实现动态联编
    virtual double area();
};

Point::Point(int x = 0, int y = 0)
{   
    cout << "construct Point ..." << endl;
    this->x = x;
    this->y = y;
}

double Point::area(){
    return 0.0;
}

Point::~Point()
{
    cout << "destruct Point ..." << endl;
}


const double PI = 3.14159;

class Circle : public Point
{
private:
    double r;
public:
    Circle(int x, int y, double r);
    ~Circle();
    double area() override;  // 父类是虚基函数，子类火覆写该方法，虚函数表vptr指针会被替换成子类的实现

    /* 虚基函数表
[0] Base::func1 的地址
[1] Base::func2 的地址
[2] Base::~Base 的地址
    
    */

};

Circle::Circle(int x, int y, double r) : Point(x, y)
{
    cout << "construct Circle ..." << endl;
    this->r = r;
}

double Circle::area(){
    return PI * r * r;
}

Circle::~Circle()
{
    cout << "destruct Circle ..." << endl;
}


int main() {
    Point p(10, 10);
    cout << "p area() = " << p.area() << endl;

    Circle c1(10, 10, 20);
    cout << "c1 area() = " << c1.area() << endl;

    Point *p1;
    p1 = &c1;
    cout << "p1->area() = " << p1->area() << endl;

    Point& p2 = c1;
    cout << "p2.area() = " << p2.area() << endl;

    return 0 ;
}

